package org.eae.eldf.webapp.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.vo.PageListVo;
import org.eae.eldf.entity.base.BaseLov;
import org.eae.eldf.framework.json.JSONArray;
import org.eae.eldf.framework.pager.Pagination;
import org.eae.eldf.service.base.BaseLovService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/******************************************************************************
 * @File name : BaseLovController.java
 * 
 * @Author : xl
 * 
 * @Date : 2016-6-7
 * 
 * @Copyright Notice: Copyright (c) 2016 Efast Serv. All Rights Reserved. This
 *            software is published under the terms of the Efast Software
 *            License version 1.0, a copy of which has been included with this
 *            distribution in the LICENSE.txt file.
 * 
 * 
 *            ------------------------------------------------------------------
 *            ---------- Date Who Version Comments 2016-6-7 上午11:00:07 xl 1.0
 *            Initial Version
 *****************************************************************************/
@Controller
@RequestMapping("/base")
public class BaseLovController extends AbstractController<BaseLov> {

	private BaseLovService baseLovService;

	@Autowired
	public void setBaseLovService(BaseLovService baseLovService) {
		this.baseLovService = baseLovService;
	}

	/**
	 * 编辑操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */
	@RequestMapping(value = "/lov/list")
	public String initPage(ModelMap modelMap, HttpServletRequest request,
			@RequestParam(required = false) String searchValue, @RequestParam(required = false) Long parentId,
			HttpServletResponse response) {
		List<BaseLov> list = baseLovService.getBaseLovListTree();
		modelMap.addAttribute("list", new JSONArray(list));
		modelMap.addAttribute("parentId", parentId);
		return "/base/lov/list";
	}

	@RequestMapping(value = "/lov/list_json")
	public @ResponseBody
	PageListVo<BaseLov> pageList(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false) Integer page, @RequestParam(required = false) Long id,
			@RequestParam(required = false) Integer rows, @RequestParam(required = false) String searchValue) {
		Pagination<BaseLov> pager = super.initPagination(request);
		// 通过菜单ID查询权限列表
		if (null != id) {
			pager = baseLovService.getBaseLovListById(pager, id);
		}
		return pageListJson(pager);
	}

	/**
	 * 编辑操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */
	@RequestMapping(value = "/lov/edit")
	public ModelAndView updateBaseLov(@ModelAttribute BaseLov baseLov, ModelAndView modelAndView) {
		baseLovService.updateBaseLov(baseLov);
		modelAndView.addObject("parentId", baseLov.getParentId());
		modelAndView.setViewName("forward:/base/lov/list");
		return modelAndView;
	}

	/**
	 * 编辑操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */
	@RequestMapping(value = "/lov/form")
	public String toLovFormPage(@RequestParam(required = false) Long id, @RequestParam(required = false) Long parentId,
			ModelMap modelMap) {
		BaseLov baseLov = null;
		if (null != id) {
			baseLov = baseLovService.get(BaseLov.class, id);
		} else {
			if (null == parentId) {
				parentId = 0L;
			}
			baseLov = new BaseLov();
			baseLov.setParentId(parentId);
		}

		modelMap.addAttribute("baseLov", baseLov);
		return "/base/lov/form";
	}

	/**
	 * 新增操作
	 * 
	 * @Date : 2016-6-7
	 * @param baseLov
	 */

	@RequestMapping(value = "/lov/add")
	public ModelAndView saveBaseLov(@ModelAttribute BaseLov baseLov, ModelAndView modelAndView) {
		baseLovService.saveBaseLov(baseLov);
		modelAndView.addObject("parentId", baseLov.getParentId());
		modelAndView.setViewName("forward:/base/lov/list");
		return modelAndView;
	}

	/**
	 * 删除操作
	 * 
	 * @Date : 2016-6-7
	 * @param basLovId
	 */

	@RequestMapping(value = "/lov/del")
	public @ResponseBody
	String deleteBaseLov(@RequestParam Long basLovId) {
		baseLovService.deleteBaseLov(basLovId);
		return "success";
	}

}
